﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace GameSPK
{
    /// <summary>
    /// Interaction logic for frmSection1Content.xaml
    /// </summary>
    public partial class frmSection1Content : Window
    {
        
        public frmSection1Content()
        {
            InitializeComponent();
        }
        //ObservableCollection<ICauHoi> cauhoithi;
        private void btnCauHoi_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                var chude = from cd in db.ChuDes
                            select cd;
                foreach (ChuDe cde in chude)
                {
                    cboChuDe.Items.Add(cde);
                }
                cboChuDe.SelectedIndex = 0;
            }
            catch
            {
            }
        }

        private void btnAddChuDe_Click(object sender, RoutedEventArgs e)
        {
            frmAddChuDe themChude = new frmAddChuDe();
            themChude.ShowDialog();
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            cboChuDe.Items.Clear();
            try
            {
                var chude = from cd in db.ChuDes
                            select cd;
                foreach (ChuDe cde in chude)
                {
                    cboChuDe.Items.Add(cde);
                }
                cboChuDe.SelectedIndex = 0;
            }
            catch
            {
            }
        }
        
        private void btnCauHoi_Click_1(object sender, RoutedEventArgs e)
        {
            
            System.Windows.Forms.OpenFileDialog OD = new System.Windows.Forms.OpenFileDialog();
            OD.Title = "Open File *.doc";
            OD.Filter = "All files (*.*)|*.*";
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            if (OD.ShowDialog() == System.Windows.Forms.DialogResult.OK)//Kiem tra khi chon mo file
            {
                //FileName = OD.FileName;
                var word = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document document = word.Documents.Open(OD.FileName);
                document.ActiveWindow.Selection.WholeStory();
                document.ActiveWindow.Selection.Copy();
                document.Close();
                IDataObject data = Clipboard.GetDataObject();
                string s = data.GetData(DataFormats.UnicodeText).ToString();
                
                taphop = Helper.LuuCauHoiPhan1(s, phanthi, ((ChuDe)cboChuDe.SelectedItem).MaCD);
                ShowData();
            }
        }
        private void ShowData()
        {
            this.gridCauHoi.DataContext = new CauHoiCollection { TapHopCauHoi = taphop };
         
        }
        ObservableCollection<ICauHoi> taphop;
        public int phanthi;
        private void cboChoose_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            General cuocthichon = GameSPK.DAO.CuocThiDAO.getCuocThiChon();
            if (cuocthichon == null)
                return;
            if (cboChoose.SelectedIndex == 0)
            {
                btnCauHoi.IsEnabled = false;
                ChuDe chudecbo;
                try
                {
                    chudecbo = (ChuDe)cboChuDe.SelectedItem;
                    taphop = CauHoiDAO.getFromChuDe(chudecbo, phanthi,cuocthichon.MaCuocChoi);
                    ShowData();
                }
                catch
                {
                }
            }
            else if(cboChoose.SelectedIndex == 1)
            {
                btnCauHoi.IsEnabled = true;
                if(taphop!=null)
                    taphop.Clear();
            }
            else if (cboChoose.SelectedIndex == 2)
            {
                
                this.lblcannhap.Content = "Tổng Câu Hỏi: " + CauHoiDAO.tongCauHoiPhanThi(phanthi, cuocthichon.MaCuocChoi) +"";
                this.lblphainhap.Content = "Câu Hỏi Cần Nhập Thêm: " + CauHoiDAO.isNhapXongCauHoi(phanthi, cuocthichon.MaCuocChoi);        
                taphop = CauHoiDAO.getDaChon(cuocthichon.MaCuocChoi, phanthi, 1);
                ShowData();
            }
        }

        private void cboChuDe_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ChuDe chudecbo;
            General cuocthichon = GameSPK.DAO.CuocThiDAO.getCuocThiChon();
            if (cuocthichon == null)
                return;
            
            try
            {
                chudecbo = (ChuDe)cboChuDe.SelectedItem;
                taphop = CauHoiDAO.getFromChuDe(chudecbo, phanthi,cuocthichon.MaCuocChoi);
                ShowData();
            }
            catch
            {
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            General cuocthichon = GameSPK.DAO.CuocThiDAO.getCuocThiChon();
            if (cuocthichon == null)
                return;
            if (CauHoiDAO.isNhapXongCauHoi(phanthi, cuocthichon.MaCuocChoi) > 0)
            {
                MessageBox.Show("Chưa nhập xong các câu hỏi");
                return;
            }

            this.Close();
            
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            //luu cau hoi lai
            General cuocthichon = GameSPK.DAO.CuocThiDAO.getCuocThiChon();
            if (cuocthichon == null)
                return;
            if (cboChoose.SelectedIndex != 2)
            {
                if (CauHoiDAO.SaveCauHoiPhanThi(taphop, cuocthichon.MaCuocChoi,1) == true)
                    MessageBox.Show("Luu thanh cong");

            }
            else
            {
                CauHoiDAO.UpdateCauHoiPhanThi(taphop, cuocthichon.MaCuocChoi);
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            General cuocthichon = GameSPK.DAO.CuocThiDAO.getCuocThiChon();
            if (cuocthichon == null)
                return;
            if (CauHoiDAO.isNhapXongCauHoi(phanthi, cuocthichon.MaCuocChoi) > 0)
            {
                MessageBox.Show("Chưa nhập xong các câu hỏi");
                return;
            }
        }
    }
}
